package com.rykj.energy.common.utils;


import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ObjectUtils {

	private static final String NULL = "null";
	public ObjectUtils() {
		super();
	}

	/**
	 * @Description: 判断是否为空
	 * @author lilong@tsinghuabigdata.com
	 * @date 2014-3-13上午10:18:20
	 * @param object
	 * @return true:为空,false:不为空
	 */
	public static boolean isEmpty(Object object) {
		if (object == null || "".equals(object) || NULL.equals(object)) {
			return true;
		}
		return false;
	}

	/**
	 * @Description: 判断是否不为空
	 * @author lilong@tsinghuabigdata.com
	 * @date 2014-3-13上午10:18:20
	 * @param object
	 * @return true:不为空,false:为空
	 */
	public static boolean isNotEmpty(Object object) {
		if (object == null || "".equals(object) || NULL.equals(object)) {
			return false;
		}
		return true;
	}
	/**
	 * 方法描述:将Object转换为String,当Object为null返回null<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2014-12-17 上午10:16:22<br/>
	 * Version:1.0 <br/>
	 * @param object
	 * @return string
	 */
	public static String toString(Object object) {
		return object!=null?object.toString():null;
	}
	/**
	 * 方法描述:将Object转换为Float,当Object为null返回null<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2015-3-16 下午2:30:53<br/>
	 * Version:1.0 <br/>
	 * @param object
	 * @return
	 */
	public static Float toFloat(Object object){
		return object!=null?Float.parseFloat(object.toString()):null;
	}
	/**
	 * 方法描述:将Object转换为Integer,当Object为null返回null<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2015-3-16 下午2:30:53<br/>
	 * Version:1.0 <br/>
	 * @param object
	 * @return
	 */
	public static Integer toInt(Object object){
		return object!=null?Integer.parseInt(object.toString()):null;
	}
	/**
	 * 方法描述:将Object转换为Date,当Object为null返回null<br/>
	 * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
	 * Date:2015-7-2 上午10:27:30<br/>
	 * Version:1.0 <br/>
	 * @param object
	 * @return
	 * @throws ParseException
	 */
	public static Date toDate(Object object) throws ParseException{
		return DateUtils.getDate(ObjectUtils.toString(object));
	}

	/**
	 * 方法描述:将Object转换为map,当Object为null返回null<br/>
	 */
	public static Map<String,Object> toMap(Object object){
		Map<String,Object> map=new HashMap<>();
		Field[] fields=object.getClass().getDeclaredFields();
		try {
			for (int i=0;i<fields.length;i++) {
				String name=fields[i].getName();
				Field field=object.getClass().getDeclaredField(name);
				field.setAccessible(true);
				if (null!=field){
					map.put(name,field.get(object).toString());
				}
			}
		}catch(Exception e){

		}
		return map;
	}

	/**
	 * 方法描述:将java类转换为map
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = Maps.newHashMap();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key + "", beanMap.get(key));
			}
		}
		return map;
	}
}
